package com.shenzhen.horselive.adapter;

import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.beans.TypeDetailsBean;

import org.jetbrains.annotations.NotNull;

import java.text.NumberFormat;

public class TypeDetailsAdapter extends BaseQuickAdapter<TypeDetailsBean.DataBean.ContentBean, BaseViewHolder> {
    public TypeDetailsAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, TypeDetailsBean.DataBean.ContentBean contentBean) {
        baseViewHolder.setText(R.id.tv_title,contentBean.getDescription())
                .setText(R.id.tv_time,contentBean.getCreateTime());
//        1：”增加帐户余额” ,3：”增加帐户的冻结余额”；4：”减少帐户的冻结余额”；
//        5：”减少帐户余额,增加帐户冻结余额”；6：”减少帐户冻结余额,增加帐户余额”
        //查询的操作类型: 1:增加操作 ,2:减少操作
        //136增加   45减少
        String  string =  "+";

        switch (contentBean.getDirection())
        {
            case 1:
            case 3:
            case 6:
                //增加
                string = "+";
                baseViewHolder.setTextColorRes(R.id.tv_red_number,R.color.red_common);
                break;
            default:
                //减少
                string = "-";
                baseViewHolder.setTextColorRes(R.id.tv_red_number,R.color.color_3);
                break;
        }
//        baseViewHolder.setText(R.id.tv_red_number,"-"+contentBean.getAmount());
        //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
        switch (contentBean.getAccountType())
        {
            case 0:
                //钱包明细
                baseViewHolder.setVisible(R.id.tv_price,false);
                baseViewHolder.setVisible(R.id.tv_total_num,false);
                baseViewHolder.setText(R.id.tv_red_number,string+formatTwoWei(contentBean.getAmount())+"元");
                break;
            case 1:
                baseViewHolder.setVisible(R.id.tv_price,false);
                baseViewHolder.setVisible(R.id.tv_total_num,false);
                baseViewHolder.setText(R.id.tv_red_number,string+formatNum(contentBean.getAmount())+"个");
                break;
            case 2:
                baseViewHolder.setVisible(R.id.tv_price,false);
                baseViewHolder.setVisible(R.id.tv_total_num,false);
                baseViewHolder.setText(R.id.tv_red_number,string+formatNum(contentBean.getAmount())+"股");
                break;
        }

    }

    /**
     *保留10位小数
     * @param num
     * @return
     */

    private  String  formatNum(String  num)
    {
        if (!TextUtils.isEmpty(num))
        {
            NumberFormat  format =NumberFormat.getInstance();
            format.setMaximumFractionDigits(10);
            return format.format(Double.parseDouble(num));

        }else
            return "0";

    }


    private String  formatTwoWei(String  num)
    {
        if (!TextUtils.isEmpty(num))
        {
            NumberFormat  format =NumberFormat.getInstance();
            format.setMaximumFractionDigits(2);
            return format.format(Double.parseDouble(num));

        }else
            return "0";
    }


}
